package net.devkit.core.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.devkit.common.toolkit.DevConstant;
import net.devkit.core.pojo.db.Dept;
import net.devkit.core.pojo.db.User;

import com.google.code.kaptcha.Constants;


public abstract class BaseController {
	
	private HttpServletRequest request;
	private HttpSession session;
	
	/**
	 * 获取request传递过来的验证码
	 * @return
	 */
	public String obtainCaptchaInRequest(){
		return request.getParameter(DevConstant.REQUEST_KAPTCHA);
	}
	
	/**
	 * 获取session里的验证码
	 * @return
	 */
	public String obtainCaptchaInSession() {
		String kaptchacode = (String) session
				.getAttribute(DevConstant.SESSION_KAPTCHA);
		// 及時清空session中的验证码，避免被重复利用
		this.clearCaptchaCode();
		return kaptchacode;
	}
	
	/**
	 * 清空session里的验证码
	 * 取出，，即清除
	 */
	public void clearCaptchaCode(){
		session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);
	}
	
	/**
	 * 将操作后的提示信息装入session
	 * @param message
	 */
	public void setSessionMessage(String message){
		session.setAttribute(DevConstant.SHOW_MESSAGESS, message);
	}
	
	
	/**
	 * 取出后，随即清空
	 * @return
	 */
	public String obtainSessionMessage(){
		final String smsg = (String) session.getAttribute(DevConstant.SHOW_MESSAGESS);
		setSessionMessage("");
		return smsg;
	}
	
	/**
	 * 获取当前用户
	 * @return
	 */
	public User obtainCurrentUser(){
		return (User) session.getAttribute(DevConstant.SESSION_CURRENT_USER);
	}
	
	/**
	 * 获取当前用户所属部门
	 * @return
	 */
	public Dept obtainCurrentDept(){
		return (Dept) session.getAttribute(DevConstant.SESSION_CURRENT_DEPT);
	}
	
	/**
	 * 如果验证码AOP未通过,一样拦截该方法的AOP是否停止执行
	 */
	public Boolean aopAbort = false;
	
	public HttpServletRequest obtainRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpSession obtainSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}
	
}
